package hikari.objects3d.helper_objects;

import org.swig.jirr.ICameraSceneNode;
import org.swig.jirr.ISceneManager;
import org.swig.jirr.ISceneNode;
import org.swig.jirr.vector3df;

/**
 * @author Dmitry
 */
public class ExtendedEditorCamera extends ISceneNode {

    private ICameraSceneNode camera;
    private ISceneNode target;

    public ExtendedEditorCamera(ISceneNode parent, ISceneManager sceneManager) {
        super(parent, sceneManager);
        camera = sceneManager.addCameraSceneNode();
        camera.setFarValue(20000f);
        target = sceneManager.addEmptySceneNode(this);
        camera.setParent(this);
        target.setParent(this);
    }

    public void updateCameraTarget() {
        updateAbsolutePosition();
        target.updateAbsolutePosition();
        camera.updateAbsolutePosition();
        camera.setTarget(target.getAbsolutePosition());
    }

    public void setRotationCenter(ISceneNode node) {
        ISceneNode root = getSceneManager().getRootSceneNode();
        vector3df cameraPosition = convertLocalVectorToGlobalSpace(camera.getPosition());
        vector3df targetPosition = convertLocalVectorToGlobalSpace(target.getPosition());
        camera.setParent(root);
        target.setParent(root);

        if (node != null) {
            super.setPosition(node.getPosition());
        } else {
            super.setPosition(new vector3df(0, 0, 0));
        }
        
        updateAbsolutePosition();
        cameraPosition = convertGlobalVectorToLocalSpace(cameraPosition);
        targetPosition = convertGlobalVectorToLocalSpace(targetPosition);
        camera.setParent(this);
        target.setParent(this);
        camera.setPosition(cameraPosition);
        target.setPosition(targetPosition);
        camera.setTarget(targetPosition);
    }

    @Override
    public void setPosition(vector3df newpos) {
        //vector3df direction = camera.getRotation().rotationToDirection();
        camera.setPosition(newpos);
        //target.setPosition(newpos.addOperator(direction));
        camera.setTarget(target.getPosition());
    }

    public ICameraSceneNode getRealCamera(){
        return camera;
    }
    
    @Override
    public void moveObjectLocalCoordinates(vector3df direction) {
        target.moveObjectLocalCoordinates(direction);
        camera.moveObjectLocalCoordinates(direction);
    }
}
